package com.bailun91.auth.mapper;


import com.bailun91.auth.dto.RoleDto;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface RoleMapper {

    /**
     * 根据用户id查询用户的角色
     * @param userId
     * @return
     */
  @Select("SELECT cu.user_id,cu.state,cu.remark,r.* FROM c_user_role cu\n" +
          "LEFT JOIN c_role r ON(cu.role_id = r.role_id)\n" +
          "WHERE cu.user_id = #{userId}; ")
  @Results(
          value = {
                  @Result(id=true, property = "id",column = "id"),
                  @Result(property = "roleId",column = "role_id"),
                  @Result(property = "name",column = "name"),
                  @Result(property = "description",column = "description"),
                  @Result(property = "status",column = "status"),
                  @Result(property = "createTime",column = "create_time"),
                  @Result(property = "updateTime",column = "update_time"),
                  @Result(property = "permissionList",column = "role_id",
                          many = @Many(select = "com.example.demo.mapper.PermissionMapper.findPermissionListByRoleId", fetchType = FetchType.DEFAULT)
                  )
          }
  )
  List<RoleDto> findRoleListByUserId(@Param("userId") Long userId);
}
